/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.core.modal.spc.TestItemSimpleModel;
import com.intelligent.ispc.client.ex.CheckBoxHeadModal;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by cherry on 2016/8/24.
 */
public class ChoiceItemTableModel extends AbstractTableModel implements CheckBoxHeadModal {

    private String[] columns;
    private List<ChoiceItemDto> choiceItemDtos;
    private List<Integer> cacheViewIndex;
    private int[] viewData;

    private TestItemSimpleModel.CheckChangeEvent changeEvent;

    @Override
    public int getRowCount() {
        if (choiceItemDtos == null) {
            return 0;
        } else {
            return choiceItemDtos.size();
        }
    }

    @Override
    public String getColumnName(int column) {
        if (columns == null) {
            return "";
        }
        return columns[column];
    }

    @Override
    public int getColumnCount() {

        if (columns == null) {
            return 0;
        }
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        if (choiceItemDtos == null || choiceItemDtos.size() <= rowIndex) {
            return new Object();
        }

        ChoiceItemDto dto = choiceItemDtos.get(rowIndex);
        if (columnIndex == 0) {
            boolean check = dto.getViewCheck() ? true : false;
            return check;
        } else if (columnIndex == 1) {
            return dto.getIndex();
        } else if (columnIndex == 2) {
            return dto.getItemName();
        } else {
            return "";
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

        if (choiceItemDtos == null || choiceItemDtos.size() <= rowIndex) {
            return;
        }

        ChoiceItemDto value = choiceItemDtos.get(rowIndex);
        switch (columnIndex) {
            case 0:
                value.setViewCheck((Boolean) aValue);
                break;
            default:
                break;
        }
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /**
     * ItemTableModel constructor
     *
     * @param columns table columns
     */
    public ChoiceItemTableModel(String[] columns) {
        if (columns == null) {
            this.columns = new String[0];
            return;
        }

        this.columns = new String[columns.length];
        System.arraycopy(columns, 0, this.columns, 0, columns.length);
        choiceItemDtos = Lists.newArrayList();
    }

    /**
     * set table data source
     *
     * @param value item table data source
     */
    public void setData(List<ChoiceItemDto> value) {

        if (value == null) {
            choiceItemDtos = Lists.newArrayList();
        } else {
            choiceItemDtos.clear();
            choiceItemDtos.addAll(value);
        }
        fireTableDataChanged();
    }

    public List<ChoiceItemDto> getData() {
        return choiceItemDtos;
    }

    /**
     * get current view selected count
     *
     * @return current view selected count
     */
    public int getSelectedCount() {
        int checkedCount = 0;
        for (ChoiceItemDto dto : choiceItemDtos) {
            if (dto.getViewCheck()) {
                checkedCount++;
            }
        }
        return checkedCount;
    }

    /**
     * set view table selected status
     *
     * @param value is selected or not
     */
    public void selectAll(boolean value) {
        for (int i = 0; i < viewData.length; i++) {
            this.setValueAt(value, viewData[i], 0);
        }

        if (changeEvent != null) {
            changeEvent.change();
        }

        fireTableDataChanged();
    }

    /**
     * set view table selected status
     *
     * @param value is selected or not
     */
    public void selectAllData(boolean value) {

        for (int i = 0; i < choiceItemDtos.size(); i++) {
            choiceItemDtos.get(i).setViewCheck(value);
        }

        fireTableDataChanged();
    }

    /**
     * @param e check change event
     */
    public void onCheckChange(TestItemSimpleModel.CheckChangeEvent e) {
        this.changeEvent = e;
    }

    /**
     * set view data table view rows
     *
     * @param viewData view data table rows
     */
    public void setViewData(int[] viewData) {
        if (viewData == null) {
            this.viewData = new int[0];
            return;
        }

        this.viewData = new int[viewData.length];
        System.arraycopy(viewData, 0, this.viewData, 0, viewData.length);
    }

    public List<Integer> getCacheViewIndex() {
        return cacheViewIndex;
    }

    public void setCacheViewIndex(List<Integer> cacheViewIndex) {
        this.cacheViewIndex = cacheViewIndex;
    }
}
